package diplom.shop.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import diplom.shop.dao.MouseDAO;
import diplom.shop.domain.Category;
import diplom.shop.domain.Motherboard;
import diplom.shop.domain.Mouse;
import diplom.shop.domain.Processor;
import diplom.shop.services.DomainServices;

public class DeviceWeb {

	public static void addDevice(List<String> devicePatam, Long categoryId ){
		int categoryIdinInt = Integer.parseInt(categoryId.toString());
		switch (categoryIdinInt) {
		case 26: addMouse(devicePatam); break;
		case 28: addProcessor(devicePatam);break;
		case 30: addMotherboard(devicePatam);break;
		
		default:
			break;
		}
	}
	public static void addMouse(List<String> mouseParam){
				
		MouseDAO mouseDAO = new MouseDAO();
		
		Category category = mouseDAO.getCategoryById(Long.parseLong(mouseParam.get(0).toString()));
		Mouse mouse = new Mouse();
		mouse.setCategory(category);
		mouse.setDeviceName(mouseParam.get(1));
		mouse.setCost(Integer.parseInt(mouseParam.get(2)));
		mouse.setGuaranty(Integer.parseInt(mouseParam.get(3)));
		//List<Image> images = 
		mouse.setButtonCount(mouseParam.get(5));
		
		mouseDAO.addMouse(mouse);
		
	}
	public static void addProcessor(List<String> processorParam){
		
		MouseDAO mouseDAO = new MouseDAO();
		
		Category category = mouseDAO.getCategoryById(Long.parseLong(processorParam.get(0).toString()));
		Processor processor = new Processor();
		processor.setCategory(category);
		processor.setDeviceName(processorParam.get(1));
		processor.setCost(Integer.parseInt(processorParam.get(2)));
		processor.setGuaranty(Integer.parseInt(processorParam.get(3)));
		//List<Image> images = 
		
		processor.setProcessorFamily(processorParam.get(5));
		processor.setSocket(processorParam.get(6));
		processor.setContactCount(processorParam.get(7));
		processor.setInternalClockFrequency(processorParam.get(8));
		processor.setCoreCount(processorParam.get(9));
		processor.setCasheFirstLevel(processorParam.get(10));
		processor.setCasheSecondLevel(processorParam.get(11));
		processor.setIntegratedGraphics(processorParam.get(12));
		
		mouseDAO.addProcessor(processor);
		
	}
public static void addMotherboard(List<String> motherboardParam){
		
		MouseDAO mouseDAO = new MouseDAO();
		
		Category category = mouseDAO.getCategoryById(Long.parseLong(motherboardParam.get(0).toString()));
		
		Motherboard motherboard = new Motherboard();
		motherboard.setCategory(category);
		motherboard.setDeviceName(motherboardParam.get(1));
		motherboard.setCost(Integer.parseInt(motherboardParam.get(2)));
		motherboard.setGuaranty(Integer.parseInt(motherboardParam.get(3)));
		//List<Image> images = 
		
		
		motherboard.setFormFactor(motherboardParam.get(5));
		motherboard.setConnectorType(motherboardParam.get(6));
		motherboard.setProcessorSupport(motherboardParam.get(7));
		motherboard.setChipsetNorthbridge(motherboardParam.get(8));
		
		motherboard.setMemorySupport(motherboardParam.get(9));
		motherboard.setMaximumMemory(motherboardParam.get(10));
		motherboard.setRaidController(motherboardParam.get(11));
		motherboard.setPowerConnector(motherboardParam.get(12));
		
		motherboard.setBuiltInVideo(motherboardParam.get(13));
		motherboard.setBuiltInAudio(motherboardParam.get(14));
		motherboard.setNumberSata2Connectors(Integer.parseInt(motherboardParam.get(15)));
		motherboard.setNumberSata3connectors(Integer.parseInt(motherboardParam.get(16)));
		
		motherboard.setExternalConnectors(motherboardParam.get(17));
		motherboard.setSlotsExpansionCards(motherboardParam.get(18));
		motherboard.setPhysicalDimensions(motherboardParam.get(19));
		
		mouseDAO.addMotherboard(motherboard);
		
	}
	public static void getMouseByName(String name){
		
		Mouse mouse = new Mouse();
		mouse.setDeviceName(name);
		MouseDAO mouseDAO = new MouseDAO();
		mouseDAO.getMouseByName(mouse);
	}
	public static void deleteMouse(String name){
		
		Mouse mouse = new Mouse();
		mouse.setDeviceName(name);
		MouseDAO mouseDAO = new MouseDAO();
		mouseDAO.deleteMouse(mouse);
	}
	public static List<Category>  getCategoryList(){
		List<Category> categoryList = DomainServices.getCategoryList();
		return categoryList;
	}
	public static Category  getCategoryById(HttpServletRequest request){
		Category category = null;
		if(request.getParameter("category")!=null){
			Long id=Long.parseLong(request.getParameter("category"));
				System.out.println(id + " request");
			category = DomainServices.getCategoryById(id);			
		}
		else if(request.getSession().getAttribute("category")!=null){// тут беретсья айді те що ми в сесії зберегли категорії
			Long id=Long.parseLong(request.getSession().getAttribute("category").toString());
				System.out.println(id + " sesion");
			category = DomainServices.getCategoryById(id);			
		}
		//тут мона впринципі дописати для діставання для перевірки ще чи є catid в сессіон
		return category;
	}
	public static boolean isCategoryIdSet(HttpServletRequest request){
		boolean isCategoryIdSet = false;
		if(request.getParameter("category")!=null){
			isCategoryIdSet=true;
		}
		//////////////
		else if(request.getSession().getAttribute("category")!=null){
			isCategoryIdSet=true;
		}
		//тут мона впринципі дописати для діставання для перевірки ще чи є catid в сессіон
		return isCategoryIdSet;
	}
}
